package handler

import (
	"Readme/soq"
	"Readme/sqite"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func Login(c *gin.Context) {
	var data soq.LoginUser
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	user, err := sqite.LoginUser(data.Mobile, data.Password)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10002,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": user,
	})

}

func AddArticle(c *gin.Context) {
	var data soq.AddArticle
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": err,
		})
		return
	}
	user, err := sqite.AddArticle(data.UserId, data.Title, data.Content, data.CType, data.GoodNum)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10002,
			"msg":  "添加失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": user,
	})
}

func ListArticle(c *gin.Context) {
	var data soq.ListArticle
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	article, err := sqite.Article(data.UserId)
	if err != nil {
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": article,
	})

}

func ListFocu(c *gin.Context) {
	var data soq.ListFocu
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	focu, err := sqite.ListFocu(data.UserId)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": focu,
	})
}

func Addfriend(c *gin.Context) {
	var data soq.AddFriend
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	friend, err := sqite.AddFriend(data.UserId)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": friend,
	})
}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "D:\\gocode\\src\\gao3\\rk\\2\\12\\chat\\Readme\\static\\upload\\" + file.Filename
	// 上传文件至指定的完整文件路径
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": dst,
	})

}
